from flask import Blueprint, render_template, request, jsonify

from applications.common import curd
from applications.common.utils import validate
from applications.common.utils.rights import authorize
from applications.common.utils.validate import str_escape
from applications.common.utils.http import table_api, success_api, fail_api
from applications.extensions import db
from applications.models import Config, User
from applications.schemas import ConfigSchema

bp = Blueprint('config', __name__, url_prefix='/config')


@bp.get('/')
@authorize("system:config:main", log=True)
def main():
    return render_template('system/config/main.html')


@bp.get('/data')
@authorize("system:config:main")
def table():
    config_name = str_escape(request.args.get('configName', type=str))
    config_key = str_escape(request.args.get('configKey', type=str))
    filters = []
    if config_name:
        filters.append(Config.config_name.contains(config_name))
    if config_key:
        filters.append(Config.config_key.contains(config_key))
    configs = Config.query.filter(*filters).layui_paginate()
    return table_api(data=ConfigSchema(many=True).dump(configs), count=configs.total)



@bp.get('/add')
@authorize("system:config:add", log=True)
def add():
    return render_template('system/config/add.html')



# 启用
@bp.put('/enable')
@authorize("system:config:edit", log=True)
def enable():
    id = request.get_json(force=True).get('Id')
    if id:
        enable = 1
        d = Config.query.filter_by(id=id).update({"enable": enable})
        if d:
            db.session.commit()
            return success_api(msg="启用成功")
        return fail_api(msg="出错啦")
    return fail_api(msg="数据错误")


# 禁用
@bp.put('/disable')
@authorize("system:config:edit", log=True)
def dis_enable():
    id = request.get_json(force=True).get('Id')
    if id:
        enable = 0
        d = Config.query.filter_by(id=id).update({"enable": enable})
        if d:
            db.session.commit()
            return success_api(msg="禁用成功")
        return fail_api(msg="出错啦")
    return fail_api(msg="数据错误")



@bp.post('/save')
@authorize("system:config:add", log=True)
def save():
    req_json = request.get_json(force=True)
    config = Config(
        enable=req_json.get('enable'),
        config_type=req_json.get('configType'),
        config_name=str_escape(req_json.get('configName')),
        config_key=str_escape(req_json.get('configKey')),
        config_value=str_escape(req_json.get('configValue')),
        remark=str_escape(req_json.get('remark'))
    )
    r = db.session.add(config)
    db.session.commit()
    return success_api(msg="成功")


@bp.get('/edit/<int:id>')
@authorize("system:config:edit", log=True)
def edit(id):
    config = curd.get_one_by_id(model=Config, id=id)
    return render_template('system/config/edit.html', config=config)



@bp.put('/update')
@authorize("system:config:edit", log=True)
def update():
    json = request.get_json(force=True)
    #id = json.get("deptId"),
    id = str_escape(json.get("Id"))
    config = {
        "enable": validate.str_escape(json.get("enable")),
        "config_type": validate.str_escape(json.get("configType")),
        "config_name": validate.str_escape(json.get("configName")),
        "config_key": validate.str_escape(json.get("configKey")),
        "config_value": validate.str_escape(json.get("configValue")),
        "remark": validate.str_escape(json.get("remark")),
    }
    d = Config.query.filter_by(id=id).update(config)
    if not d:
        return fail_api(msg="更新失败")
    db.session.commit()
    return success_api(msg="更新成功")


@bp.delete('/remove/<int:_id>')
@authorize("system:config:remove", log=True)
def remove(_id):
    res = Config.query.filter_by(id=_id).delete()
    if not res:
        return fail_api(msg="删除失败")
    db.session.commit()
    return success_api(msg="删除成功")


